package com.wb.spring.boot.autoconfigure.core;

import com.wb.spring.boot.autoconfigure.client.WbClientSpecification;
import com.wb.spring.boot.autoconfigure.client.WbClientsConfiguration;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.cloud.context.named.NamedContextFactory;
import org.springframework.lang.Nullable;

import java.util.Map;

/**
 * @author sw
 * @create 2023-03-03 11:03
 */
public class WbContext extends NamedContextFactory<WbClientSpecification> {

    public WbContext() {
        super(WbClientsConfiguration.class, "simple.feign", "client.name");
    }

    @Nullable
    public <T> T getInstanceWithoutAncestors(String name, Class<T> type) {
        try {
            return BeanFactoryUtils.beanOfType(getContext(name), type);
        } catch (BeansException ex) {
            return null;
        }
    }

    @Nullable
    public <T> Map<String, T> getInstancesWithoutAncestors(String name, Class<T> type) {
        return getContext(name).getBeansOfType(type);
    }

}
